package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.Person;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.ProcessAttachment;
import com.smedi.ismedi.inmail.core.domain.dto.ProcessAttachmentQuery;
import com.smedi.ismedi.inmail.core.domain.repository.ProcessAttachmentRepository;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.ProcessAttachmentEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.ProcessAttachmentMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 主体院关系实现类
 *
 * @author niuyanling
 * @date 2021/08/05
 */
@Repository
@AllArgsConstructor
public class ProcessAttachmentRepositoryImpl implements ProcessAttachmentRepository {
    private final AdapterConverter adapterConverter;
    private final VerifyHelper verifyHelper;

    private final ProcessAttachmentMapper processAttachmentMapper;

    @Override
    public ProcessAttachment getProcessAttachmentOne(ProcessAttachmentQuery processAttachmentQuery) {
        LambdaQueryWrapper<ProcessAttachmentEntity> lambdaQueryWrapper = this.ProcessAttachmentQueryToLambdaQueryWrapper(processAttachmentQuery);

        ProcessAttachmentEntity processAttachmentEntity = processAttachmentMapper.selectOne(lambdaQueryWrapper);

        return adapterConverter.processAttachmentEntityToProcessAttachment(processAttachmentEntity);
    }

    @Override
    public List<ProcessAttachment> getProcessAttachmentList(ProcessAttachmentQuery processAttachmentQuery) {
        LambdaQueryWrapper<ProcessAttachmentEntity> lambdaQueryWrapper = this.ProcessAttachmentQueryToLambdaQueryWrapper(processAttachmentQuery);

        List<ProcessAttachmentEntity> processAttachmentEntities = processAttachmentMapper.selectList(lambdaQueryWrapper);

        return adapterConverter.processAttachmentEntityToProcessAttachment(processAttachmentEntities);
    }

    private LambdaQueryWrapper<ProcessAttachmentEntity> ProcessAttachmentQueryToLambdaQueryWrapper(ProcessAttachmentQuery processAttachmentQuery) {
        LambdaQueryWrapper<ProcessAttachmentEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processAttachmentQuery.getId()), ProcessAttachmentEntity::getId, processAttachmentQuery.getId());
        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processAttachmentQuery.getIds()), ProcessAttachmentEntity::getId, processAttachmentQuery.getIds());
        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processAttachmentQuery.getTaskId()), ProcessAttachmentEntity::getTaskId, processAttachmentQuery.getTaskId());
        lambdaQueryWrapper.in(verifyHelper.isAvailable(processAttachmentQuery.getTaskIds()), ProcessAttachmentEntity::getTaskId, processAttachmentQuery.getTaskIds());

        return lambdaQueryWrapper;
    }
}